package com.hnkywl.sts.service.organ;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.base.SjzdzDao;
import com.hnkywl.sts.dao.organ.WlsDao;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Wls;
import com.hnkywl.sts.entity.organ.Ysgj;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午2:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class WlsService {
    private WlsDao wlsDao;
    private SjzdzDao sjzdzDao;
    private BmService bmService;

    /**
     * 查询物流商列表
     *
     * @param qc       物流商名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllWls(String qc, String jc, String lxr, int pageNo, int pageSize) {
        Page<Wls> wlsPage = this.wlsDao.findWlsList(qc, jc, lxr, pageNo, pageSize);
        List<Wls> wlsList = wlsPage.getResult() == null ? new ArrayList<Wls>() : wlsPage.getResult();
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("list_ysgjList", "ysgjsbm");
        String wlsJson = BaseUtils.fetchCollectionToJson(wlsList, otherParam, "id", "qc", "jc", "bz", "sfzx", "qyfr.qyqc", "qyfr.id", "lxr", "ysgjList", "zt", "processInstanceId");
        return "{\"total\":" + wlsPage.getTotalCount() + ",\"rows\":[" + wlsJson + "]}";
    }

    /**
     * 物流商科目代码
     */
    public String findKmdm(String qc, String jc, int pageNo, int pageSize) {
        Page<Wls> wlsPage = this.wlsDao.findWlsList(qc, jc, null, pageNo, pageSize);
        List<Wls> gsjgList = wlsPage.getResult() != null ? wlsPage.getResult() : new ArrayList<Wls>();
        List<Map> grid = new ArrayList<Map>();
        for (Wls gsjg : gsjgList) {
            Map item = new HashMap();
            item.put("wls.id", gsjg.getId());
            item.put("wls.jc", gsjg.getJc());
            item.put("wls.qc", gsjg.getJc());
            item.put("wls.bm", gsjg.getBm());
            item.put("wls.bz", gsjg.getBz());
            item.put("wls.wlsKmdm", gsjg.getWlsKmdm());
            grid.add(item);
        }
        return "{\"total\":" + wlsPage.getTotalCount() + ",\"rows\":" + new Gson().toJson(grid) + "}";
    }


    /**
     * 所有返回 企业机构名称
     *
     * @return
     */
    @Transactional
    public String findWlsLxr() {
        List<Wls> wlsList = wlsDao.findAllRootWls() == null ? new ArrayList<Wls>() : wlsDao.findAllRootWls();
        String wlsJson = BaseUtils.fetchCollectionToJson(wlsList, "id", "qc", "lxr");
        return "[" + wlsJson + "]";
    }

    /**
     * 保存科目代码
     *
     * @param wls
     */
    @Transactional
    public void saveKmdm(Wls wls) {
        Wls w = wlsDao.get(wls.getId());
        w.setWlsKmdm(wls.getWlsKmdm());
        wlsDao.save(w);
    }


    /**
     * 查询物流商明细
     *
     * @param id 物流商id
     * @return
     */
    @Transactional
    public Wls findWlsDetail(Long id) {
        return this.wlsDao.get(id);
    }

    public String getJgbm() {
        return bmService.getJgbm();
    }

    /**
     * 保存物流商那信息
     *
     * @param wls 物流商实体
     * @return
     */
    @Transactional
    public void saveWls(Wls wls) throws RuntimeException {
        nullId(wls);
        if (wlsDao.checkUnique(wls.getId(), wls.getQc(), "qc")) {
            throw new RuntimeException("此全称已存在");
        }
        if (wlsDao.checkUnique(wls.getId(), wls.getJc(), "jc")) {
            throw new RuntimeException("简称已存在");
        }
        if (wlsDao.checkUnique(wls.getId(), wls.getBm(), "bm")) {
            throw new RuntimeException("编码重复");
        }
        String bm = this.getJgbm();
        wls.setBm(bm);
        wls.setZt("ybc");
        wls.setDateCreated(new Date());
        wls.setLastUpdated(wls.getDateCreated());
        if (wls.getYsgjList() != null) {
            for (Ysgj ysgj : wls.getYsgjList()) {
                ysgj.setWls(wls);
                Sjzdz ysgjlx = sjzdzDao.getSjzdzIdByBz("ysgjlx", ysgj.getYsgjlx().getZ());
                ysgj.setYsgjlx(ysgjlx);
            }
        }

        wls.setSfzx(!wls.getSfzx());
        this.wlsDao.save(wls);
    }

    @Transactional
    public void deleteWls(Wls wls) {
        if (!wls.getSfzx()) {
            throw new RuntimeException("禁止删除！机构已启用");
        }
        if (wls.getGsjgList() != null && wls.getGsjgList().size() != 0) {
            throw new RuntimeException("禁止删除！存在子机构");
        }
        if (wls.getYgList() != null && wls.getYgList().size() != 0) {
            throw new RuntimeException("禁止删除！存在员工");
        }
        wls.setLastUpdated(new Date());
        wls.setDeleted(1);
        this.wlsDao.save(wls);
    }

    @Transactional
    public void updateWls(Wls wls) throws RuntimeException {
        if (wls.getYsgjList() != null) {
            for (Ysgj ysgj : wls.getYsgjList()) {
                ysgj.setWls(wls);
                Sjzdz ysgjlx = sjzdzDao.getSjzdzIdByBz("ysgjlx", ysgj.getYsgjlx().getZ());
                ysgj.setYsgjlx(ysgjlx);
            }
        }
        wls.setSfzx(!wls.getSfzx());
        Wls t = wlsDao.get(wls.getId());
        t.setQc(wls.getQc());
        t.setJc(wls.getJc());
        t.setBm(wls.getBm());
        t.setZjm(wls.getZjm());
        t.setQyfr(wls.getQyfr());
        t.setBz(wls.getBz());
        t.setLxr(wls.getLxr());
        t.setSfzx(wls.getSfzx());
        t.setYsgjList(wls.getYsgjList());
        nullId(t);
        this.wlsDao.save(t);
    }

    /**
     * 关联判断
     *
     * @param wls
     */
    private void nullId(Wls wls) {
        if (wls != null && wls.getQyfr() != null && wls.getQyfr().getId() == null)
            wls.setQyfr(null);
    }

    /**
     * 返回所有的物流商的组织树
     *
     * @return
     */
    public String findAllWlsTree() {
        List<Wls> wlsList = new ArrayList<Wls>();
        wlsList.addAll(wlsDao.findAllRootWls());
        Map<String, String> treeParam = new HashMap<String, String>();
        treeParam.put("text", "qc");
        treeParam.put("id", "id");
        return BaseUtils.getTreeJson(wlsList, treeParam);
    }

    /**
     * 返回所有的物流商的组织树
     *
     * @return
     */
    public String findAllWlsJcTree() {
        List<Wls> wlsList = new ArrayList<Wls>();
        wlsList.addAll(wlsDao.findAllRootWls());
        Map<String, String> treeParam = new HashMap<String, String>();
        treeParam.put("text", "jc");
        treeParam.put("id", "id");
        return BaseUtils.getTreeJson(wlsList, treeParam);
    }

    @Autowired
    public void setWlsDao(WlsDao wlsDao) {
        this.wlsDao = wlsDao;
    }

    @Autowired
    public void setSjzdzDao(SjzdzDao sjzdzDao) {
        this.sjzdzDao = sjzdzDao;
    }

    /**
     * 查询启用的物流商
     *
     * @return
     */
    public List<Wls> findeAllInuse() {
        return this.wlsDao.findAllInuse();
    }

    //所有物流商
    @Transactional
    public List<Wls> findAllWls() {
        return this.wlsDao.findAllWls();
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
}
